package com.spring.AoP5_4;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component          //申请成为组件
@Aspect             //定义一个切面
public class Advice {
    //这是AoP编程的主要class

    //定义切入点
    @Pointcut("execution(* com.spring.AoP5_4..*.*(..))")
    private void fun1(){

    }

//    下面的方法都是对应切入点的逻辑
//    而这些逻辑中，执行的先后顺序是 Around->Before->AfterReturning->AfterThrowing->After->Around
    @Before(value = "fun1()")
    public void adviceBefore(JoinPoint jp){
        System.out.println("-------Before-------------");
        System.out.println("方法名："+jp.getSignature().getName()+
                "\n参数长度:"+jp.getArgs().length+
                "\n被代理对象："+jp.getTarget().getClass().getName());
    }

    @Around("fun1()")
    public Object adviceArounde(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("------环绕开始-----------");
        Object object = pjp.proceed();
        System.out.println("----------环绕结束-------");
        return object;
    }

    @After("fun1()")
    public void adviceAfter(){
        System.out.println("-------After-------");
    }

    @AfterThrowing("fun1()")
    public void afterThrowing(){
        System.out.println("抛出异常后通知");
    }

    @AfterReturning("fun1()")
    public void afterRetuen(){
        System.out.println("返回后通知");
    }



//    *****定义新的切点*****

//    @Pointcut("execution(* com.spring.ioc5_3..*.*(..))")
//    private void fun2(){}
//
//    @Before("fun2()")
//    public void fun2Before(){
//        System.out.println("------Befor--------");
//    }
}
